Page({
  data: {
    currentTab: 'words', // 'words' 或 'articles'
    wordsCollection: [],
    articlesCollection: [],
    currentCollection: []
  },

  onLoad: function(options) {
    // 检查是否有指定的标签参数
    if (options.tab) {
      this.setData({
        currentTab: options.tab
      });
    }
    this.loadCollections();
  },

  onShow: function() {
    this.loadCollections();
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },

  // 加载收藏数据
  loadCollections: function() {
    // 加载单词收藏
    const wordsCollection = wx.getStorageSync('wordsCollection') || [];
    
    // 加载文章收藏
    const articlesCollection = wx.getStorageSync('collectedArticles') || [];
    
    this.setData({
      wordsCollection: wordsCollection,
      articlesCollection: articlesCollection,
      currentCollection: this.data.currentTab === 'words' ? wordsCollection : articlesCollection
    });
  },

  // 切换标签
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    const collection = tab === 'words' ? this.data.wordsCollection : this.data.articlesCollection;
    
    this.setData({
      currentTab: tab,
      currentCollection: collection
    });
  },

  // 查看单词详情
  viewWordDetail: function(e) {
    const word = e.currentTarget.dataset.word;
    wx.navigateTo({
      url: `/pages/word-detail/word-detail?word=${word}`
    });
  },

  // 查看文章详情
  viewArticleDetail: function(e) {
    const articleId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/article-detail/article-detail?id=${articleId}`
    });
  },

  // 删除单词收藏
  removeWordCollection: function(e) {
    const word = e.currentTarget.dataset.word;
    
    wx.showModal({
      title: '确认删除',
      content: `确定要取消收藏单词"${word}"吗？`,
      success: (res) => {
        if (res.confirm) {
          let wordsCollection = wx.getStorageSync('wordsCollection') || [];
          wordsCollection = wordsCollection.filter(item => item.word !== word);
          wx.setStorageSync('wordsCollection', wordsCollection);
          
          this.setData({
            wordsCollection: wordsCollection,
            currentCollection: this.data.currentTab === 'words' ? wordsCollection : this.data.currentCollection
          });
          
          wx.showToast({
            title: '取消收藏成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 删除文章收藏
  removeArticleCollection: function(e) {
    const articleId = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '确认删除',
      content: '确定要取消收藏这篇文章吗？',
      success: (res) => {
        if (res.confirm) {
          let articlesCollection = wx.getStorageSync('collectedArticles') || [];
          articlesCollection = articlesCollection.filter(item => item.id !== articleId);
          wx.setStorageSync('collectedArticles', articlesCollection);
          
          this.setData({
            articlesCollection: articlesCollection,
            currentCollection: this.data.currentTab === 'articles' ? articlesCollection : this.data.currentCollection
          });
          
          wx.showToast({
            title: '取消收藏成功',
            icon: 'success'
          });
        }
      }
    });
  }
}) 